This sounds a bit posh, but it really means operators which can be applied to a single operand. These can be confusing, in that unless you know what the operator means you won't know what it does. However, for completeness, and because you will often find programs which contain them, here are how they work.

So far the operators which we have seen work between two operands:

fred + 1

In this case the operator + is applied between the constant 1 and the variable Fred The result of the expression is the value of Fred plus one. We have already seen the line:

count = count + 1 ;

This has the effect of making the value in count one bigger. C provides a unary operator which does this:

count++ ;

The ++ is an operator which causes the compiler to increment the given variable. This is a good idea, in that sometimes this can be done more quickly than the assignment which we saw above.

There is a complementary version which makes variables smaller:

count-- ;

C has a final trick with unary operators, in that it lets you combine their use with other operations, so that you can combine an update with a test. When you are doing this the position of the -- or ++ operator is important:

count++  /* value used is count */
++count  /* value used count + 1 */

When the unary operator (++ or --) is given before the variable C will perform the update before supplying the value for something to look at. When the operator is given after the variable C will provide the value before the update is performed.

Unary operators are often used in for loops, where they let you quickly express the update to be performed on a control variable (see later). However, you do not need to use them if you don't want to and I will not be using them much in example programs which I write.

See the program to the right, try and work out what the value will be in each case. Remember that the position of the ++ or -- affects the value used.